﻿using System;
using System.Web.UI.WebControls;
using DBProvider.Providers;
using DBProvider;
using System.Collections.Generic;
using System.Linq;

namespace HealthCommunity.Controls
{
	public partial class UCExaminationList : System.Web.UI.UserControl
	{
		private IExaminationProvider _ExaminationProvider;
		private List<int> _UsersExamination;

		private IList<Examination> _DataSource;
		private int _TotalRecords;

		public IList<Examination> DataSourse
		{
			get
			{
				var pageIndex = this.PageIndex;
				var totalRecords = 0;
				var pageSize = this.PageSize;

				var selectedExmtCatID = int.Parse(ddlExmtCat.SelectedValue);

				if(_ExaminationProvider == null)
				{
					_ExaminationProvider = new ExaminationProvider();
				}

				var user = SiteContext.Current.CurrentUser;

				if(user != null && user.Role.RoleName.ToLower().Equals("admin"))
				{
					var drugStore = SiteContext.Current.CurrentDrugStore;

					if(drugStore != null)
					{
						var lstUserAndExmt = _ExaminationProvider.GetUserAndExaminationByUser(user.UserID);

						if(lstUserAndExmt != null && lstUserAndExmt.Count > 0)
						{
							_UsersExamination = lstUserAndExmt.Select(ue => ue.ExaminationID).ToList();
						}

						_DataSource = _ExaminationProvider.GetExaminationByCategory(selectedExmtCatID, drugStore.DrugStoreID, pageIndex, pageSize, out totalRecords);
					}
				}
				else
				{
					if(user != null && user.DrugStoreID.GetValueOrDefault(0) > 0)
					{
						var lstUserAndExmt = _ExaminationProvider.GetUserAndExaminationByUser(user.UserID);

						if(lstUserAndExmt != null && lstUserAndExmt.Count > 0)
						{
							_UsersExamination = lstUserAndExmt.Select(ue => ue.ExaminationID).ToList();
						}

						_DataSource = _ExaminationProvider.GetExaminationByCategory(user.UserID, selectedExmtCatID, user.DrugStoreID.Value, pageIndex, pageSize, out totalRecords);
					}
				}

				this.TotalRecords = totalRecords;

				return _DataSource;
			}
		}

		public int PageIndex
		{
			get
			{
				if(ViewState["PageIndex"] == null)
				{
					return 0;
				}
				else
				{
					return int.Parse(ViewState["PageIndex"].ToString());
				}
			}
			set
			{
				ViewState["PageIndex"] = value;
			}
		}

		public int TotalRecords
		{
			get
			{
				return _TotalRecords;
			}
			set
			{
				_TotalRecords = value;
			}
		}

		public int TotalPages
		{
			get
			{
				var data = _DataSource == null ? this.DataSourse : _DataSource;

				if(data == null)
				{
					return 0;
				}

				int totalRecords = TotalRecords;

				int totalPages = 0;

				if(totalRecords <= PageSize)
				{
					totalPages = 1;
				}

				if(totalRecords % PageSize == 0)
				{
					totalPages = totalRecords / PageSize;
				}

				if(totalRecords % PageSize > 0)
				{
					totalPages = totalRecords / PageSize + 1;
				}

				return totalPages;
			}
		}

		public int PageSize
		{
			get
			{
				return int.Parse(ddlPageSize.SelectedValue);
			}
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if(!IsPostBack)
			{
				BindExaminationCategory();
			}
		}

		protected void Page_PreRender(object sender, EventArgs e)
		{
			hlCreateExmt.Visible = false;

			BindExamination();

			var currentPageIndex = this.PageIndex;
			var totalPages = TotalPages;

			// set pager index links
			List<PagerIndex> indexs = new List<PagerIndex>();

			if(totalPages <= 25)
			{
				for(int i = 1; i <= totalPages; i++)
				{
					indexs.Add(new PagerIndex(i.ToString()));
				}
			}
			else
			{
				for(var i = 13; i > 0; i--)
				{
					if(indexs.Count >= 27)
					{
						break;
					}

					var index = currentPageIndex - i;

					if(index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}

				for(var i = 0; i <= 13; i++)
				{
					if(indexs.Count > 27)
					{
						break;
					}

					var index = currentPageIndex + i;

					if(index <= totalPages && index > 0)
					{
						indexs.Add(new PagerIndex(index.ToString()));
					}
				}
			}

			this.RptrPageIndex.DataSource = indexs;
			this.RptrPageIndex.DataBind();

			this.LabelTotalPages.Text = totalPages.ToString();
			this.LabelTotalRecords.Text = TotalRecords.ToString();

			// disenabel the current pagerindex link
			if(RptrPageIndex.Items.Count > 0)
			{
				for(int i = 0; i < RptrPageIndex.Items.Count; i++)
				{
					LinkButton thePagerIndexLink = RptrPageIndex.Items[i].FindControl("LBPageIndex") as LinkButton;

					if(thePagerIndexLink.Text.Trim().Equals((currentPageIndex + 1).ToString()))
					{
						thePagerIndexLink.Enabled = false;
					}
				}
			}

			lbtnFirstPage.Enabled = (currentPageIndex > 0);

			lbtnLastPage.Enabled = totalPages > 1 && (currentPageIndex != totalPages - 1);

			var user = SiteContext.Current.CurrentUser;

			if(user != null)
			{
				if(user.Role != null)
				{
					hlCreateExmt.Visible = user.Role.RoleName.ToLower().Equals("admin") || user.Role.RoleName.ToLower().Equals("storemanager") || user.Role.RoleName.ToLower().Equals("storeadmin");
				}
			}

		}

		protected void gvExmt_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			if(e.CommandName == "remove")
			{
				//var lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;

				var id = int.Parse(e.CommandArgument.ToString());

				if(_ExaminationProvider == null)
				{
					_ExaminationProvider = new ExaminationProvider();
				}

				if(_ExaminationProvider.DeleteExamination(id))
				{
					ltrMsg.Text = "删除成功";
					//BindExamination();
				}
				else
				{
					ltrMsg.Text = "删除失败了";
				}
			}
		}

		protected void gvExmt_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if(e.Row.DataItem != null)
			{
				var dataItem = e.Row.DataItem as Examination;
				var hlJoinExmt = e.Row.FindControl("hlJoinExmt") as HyperLink;
				var hlEditExmt = e.Row.FindControl("hlEditExmt") as HyperLink;
				var lbtnDelete = e.Row.FindControl("lbtnDelete") as LinkButton;

				hlEditExmt.NavigateUrl = "~/ExaminationEdit.aspx?examinationid=" + dataItem.ExaminationID.ToString();
				hlJoinExmt.NavigateUrl = "~/ExaminationDetail.aspx?examinationid=" + dataItem.ExaminationID.ToString();

				lbtnDelete.CommandArgument = dataItem.ExaminationID.ToString();

				var hasExam = (_UsersExamination != null && _UsersExamination.Count > 0 && _UsersExamination.Contains(dataItem.ExaminationID));

				if(hasExam)
				{
					hlJoinExmt.Text = "查看结果";
				}
				else
				{
					hlJoinExmt.Text = "参加考试";
				}

				lbtnDelete.Visible = false;
				hlEditExmt.Visible = false;
				var user = SiteContext.Current.CurrentUser;

				if(user != null && user.Role != null)
				{
					lbtnDelete.Visible = user.Role.RoleName.ToLower().Equals("storeadmin") || user.Role.RoleName.ToLower().Equals("storemanager") || user.Role.RoleName.ToLower().Equals("admin");
					hlEditExmt.Visible = user.Role.RoleName.ToLower().Equals("storeadmin") || user.Role.RoleName.ToLower().Equals("storemanager") || user.Role.RoleName.ToLower().Equals("admin");
				}
			}
		}

		protected void LBPageIndex_Command(object sender, System.Web.UI.WebControls.CommandEventArgs e)
		{
			switch(e.CommandName)
			{
				case "PageIndex":
					{
						LinkButton theLink = sender as LinkButton;

						int index = int.Parse(theLink.Text);

						PageIndex = index - 1;

						//TotalRecords = DataSource.Count();

						BindExamination();
					}
					break;
			}
		}

		protected void lbtnFirstPage_Click(object sender, EventArgs e)
		{
			PageIndex = 0;
			BindExamination();
		}

		protected void lbtnLastPage_Click(object sender, EventArgs e)
		{
			PageIndex = TotalPages - 1;
			BindExamination();
		}

		protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
		{
			this.PageIndex = 0;
		}

		protected void ddlExmtCat_SelectedIndexChanged(object sender, EventArgs e)
		{
			//BindExamination();
		}

		private void BindExamination()
		{
			var data = this.DataSourse;

			gvExmt.DataSource = data;
			gvExmt.DataBind();
		}

		private void BindExaminationCategory()
		{
			if(_ExaminationProvider == null)
			{
				_ExaminationProvider = new ExaminationProvider();
			}

			ddlExmtCat.Items.Clear();
			ddlExmtCat.DataSource = _ExaminationProvider.GetAllExaminationCategory();
			ddlExmtCat.DataBind();

			ddlExmtCat.Items.Insert(0, new ListItem("所有类别", "0"));
			ddlExmtCat.SelectedIndex = 0;
		}
	}
}